package brightgrove.pages;

import brightgrove.model.Film;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.ui.ExpectedConditions;

import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated;

/**
 * Created by alaktionov on 21.10.2014.
 */
public class UserHomePage extends InternalPage {

    public UserHomePage(PageManager pages) {
        super(pages);
    }

    @FindBy(css = "#content > section > nav > ul > li:nth-child(1) > div > div > a > img")
    private WebElement addButton;

    @FindBy(css = "#content > section > nav > ul > li:nth-child(2) > div > div > a > img")
    private WebElement updateButton;

    @FindBy(css = "#content > section > nav > ul > li:nth-child(3) > div > div > a > img")
    private WebElement exportButton;

    @FindBy(id = "q")
    private WebElement searchField;

    @FindBy(id = "movie_58")
    private WebElement filmName;

    public WebElement getAddButton() {
        return addButton;
    }

    public void clickFilm(String name) {
        filmName.click();
    }

    public void enterToSearchField(String name) {
        searchField.sendKeys(name);
    }

    public void submitSearchRequest() {
        wait.until(ExpectedConditions.textToBePresentInElement(searchField,searchField.getAttribute("value")));
        searchField.sendKeys(Keys.ENTER);
    }

    public UserHomePage ensurePageLoaded() {
        super.ensurePageLoaded();
        wait.until(presenceOfElementLocated(By.cssSelector("#content > section > nav")));
        return this;
    }

    public void clickAddButton() {
        addButton.click();
    }


}
